"""
本文件用于从mongo中查询数据，并将task_id 增加到redis中，用于过滤去重。

"""

#_*_coding:utf-8_*_

import sys
import os
import json

project_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
print(project_path)
sys.path.append(project_path)

# 打印所有python解释器可以搜索到的所有路径
print(sys.path)


import json
import time
import asyncio

from Freepatentsonline.databases.mongo_handler import Mongo_handler
from Freepatentsonline.databases.redis_handler import Aredis_handler
from Freepatentsonline.utils.path_file import get_config
from Freepatentsonline.settings import CONFIG_DIR, CONFIG_FILENAME, DB_CONFIG_FILENAME




CONFIG = get_config(CONFIG_DIR, CONFIG_FILENAME)
# 数据库配置文件
DB_CONFIG = get_config(CONFIG_DIR, DB_CONFIG_FILENAME)


# 调度 redis存储数据库配置信息
REDIS_SCHEDULER = DB_CONFIG["REDIS_SCHEDULER"]
# 获取到 redis 存储数据库配置信息
REDIS_STORE = DB_CONFIG["REDIS_STORE"]

# 获取到 mongo 存储数据库配置信息
MONGO_CONFIG = DB_CONFIG["MONGO"]

REDIS_STORE_DATA = DB_CONFIG["REDIS_STORE_DATA"]


class Update_finished:
    def __init__(self) -> None:
        self.redis_scheduler = Aredis_handler(REDIS_SCHEDULER)
        self.mongo = Mongo_handler(MONGO_CONFIG)

        loop = asyncio.get_event_loop()
        loop.run_until_complete(self.run())

    async def run(self):
        collection_names = self.mongo.db.list_collection_names()

        for collection_name in collection_names:
            # collection_name = _collection["name"]
            print(f"当前collection :{collection_name}")
            collection = self.mongo.db[collection_name]
            # 查询数据
            datas = collection.find({}, {"task_id": 1, "group_finish":1, "_id": 0 })

            for data in datas:
                await self.redis_scheduler.redis.sadd(data["group_finish"],data["task_id"])


if __name__ == "__main__":
    update_finished = Update_finished()